var myDao = require('../lib/myDao');


//测试一:多行sql语句,不带参数
function test1(fn) {
    var sql1 = 'drop table if exists user;';
    var sql2 = 'create table user(id integer auto_increment primary key ,name varchar(255),age int ,intro text);';
    var sql3 = 'insert into user (name,age,intro) values("wang",11,"wangIntro");';
    var sql4 = 'select * from user;';
    var sqls = sql1 + sql2 + sql3 + sql4;
    myDao.exeSqls(undefined, sqls, '', function (err, data) {
        console.log(err, data);
        fn();
    })
}


//测试三:一行语句,带一行参数
function test2(fn) {
    var sql4 = 'select * from user;';
    var sql = 'insert into user (name,age,intro) values(?,?,?)';
    var pm = ['zhao', 22, 'zhaoIntro'];
    myDao.exeSqls(undefined, sql, pm, function (err, data) {
        console.log(err, data);
        myDao.exeSqls(undefined, sql4, null, function (err, data) {
            console.log(err, data);
            fn();
        })
    })
}


//测试三:一行语句,带多行参数
function test3(fn) {
    var sql4 = 'select * from user;';
    var sql = 'insert into user (name,age,intro) values(?,?,?)';
    var pms = [
        ['zhang', 33, 'zhangIntro'],
        ['li', 44, 'liIntro']
    ];
    myDao.exeSqls(undefined, sql, pms, function (err, data) {
        console.log(err, data);
        myDao.exeSqls(undefined, sql4, null, function (err, data) {
            console.log(err, data);
            fn();
        })
    })
}

function getAll() {
    myDao.getByWhere('', '', function (err, data) {
        console.log(err, data);
    })
}


function getById() {
    myDao.getById('', {id: 3}, function (err, data) {
        console.log(err, data);
    })
    myDao.getById('', {id: 3, keys: 'id,name'}, function (err, data) {
        console.log(err, data);
    })
}


function getByIds() {
    myDao.getByIds('', {ids: '1,2,3'}, function (err, data) {
        console.log(err, data);
    })
    myDao.getByIds('', {ids: '1,2,3', keys: 'id,name', order: 'name desc', pageSize: 2, pageIndex: 1}, function (err, data) {
        console.log(err, data);
    })
}


function getByWhere() {

    myDao.getByWhere('', {where: 'name like "%an%"', keys: 'id,name', order: 'name desc', pageSize: 2, pageIndex: 0}, function (err, data) {
        console.log(err, data);
    })
}


function add() {
    myDao.add('', {row: {name: 'zz', age: 3}}, function (err, data) {
        console.log(err, data)
        getAll();
    })
}

function adds() {
    myDao.adds('', {rows: [
        {name: 'zz', age: 3},
        {name: 'yy', age: 4}
    ]}, function (err, data) {
        console.log(err, data)
        getAll();
    })
}


function addsCsv() {
    myDao.addsByCsv('', {
        keys: ['name', 'age'],
        rows: [
            ['xx', 5],
            ['uu', 6]
        ]}, function (err, data) {
        console.log(err, data)
        getAll();
    })
}

function editById() {
    myDao.editById('', {id: 1, row: {name: 'ssssss'}}, function (err, data) {
        console.log(err, data);
        getAll();
    })
}
function editByIds() {
    myDao.editByIds('', {ids: '1,2,3', row: {name: 'ssssss'}}, function (err, data) {
        console.log(err, data);
        getAll();
    })
}

function editByWhere() {
    myDao.editByWhere('', {where: 'id<?', pms: [4], row: {name: 'vvvvvv'}}, function (err, data) {
        console.log(err, data);
        getAll();
    })
}

function delById() {
    myDao.delById('', {id: 1}, function (err, data) {
        console.log(err, data);
        getAll();
    })
}

function delByIds() {
    myDao.delByIds('', {ids: '1,2,3'}, function (err, data) {
        console.log(err, data);
        getAll();
    })
}


function delByWhere() {
    myDao.delByWhere('', {where: 'name like ?', pms: ['%z%']}, function (err, data) {
        console.log(err, data);
        getAll();
    })
}

function getCountSumByWhere() {
    var pm = {
        where: 'id>?',
        pms: [1],
        keys: 'count(*) as total,sum(age) as sum_age'
    };
    myDao.getCountSumByWhere('', pm, function (err, data) {
        console.log(err, data);
        getAll();
    })
}

function addNumById() {
    var pm = {id: 9, row: {age: 33}};
    myDao.addNumById('', pm, function (err, data) {
        console.log(err, data);
        getAll();
    })
}

function addNumByIds() {
    var pm = {ids: '9', row: {age: 33}};
    myDao.addNumByIds('', pm, function (err, data) {
        console.log(err, data);
        getAll();
    })
}

function addNumByWhere() {
    var pm = {where: 'id<?', pms: [9], row: {age: 33}};
    myDao.addNumByWhere('', pm, function (err, data) {
        console.log(err, data);
        getAll();
    })
}

function main() {
//    test1(function () {
//        test2(function () {
//            test3(function () {
//            });
//        })
//    })

//    getAll();
//    getById();
//    getByIds();
//    getByWhere();
//    add();
//    adds();
//    addsCsv();
//    editById();
//    editByIds();
//    editByWhere();
//    delById();
//    delByIds();
//    delByWhere()
//    getCountSumByWhere();
//    addNumById();
//    addNumByIds();
//    addNumByWhere();


}

main();

